import streamlit as st

def main():
    '''变量持久化之初始化，赋空值，避免报错'''
    sslist = []
    # 01_Data_Preview
    sslist = sslist + ['P01_datafile_info', 'P01_datafile_name', 'P01_preview_rows', 
                       'P01_seperator', 'P01_encoding', 'P01_headfile_info', 
                       'P01_headfile_name', 'P01_firstline', 'P01_sql']
    
    # 02_SQL_Analysis
    sslist = sslist + ['P02_datafile_info', 'P02_datafile_name', 'P02_preview_rows', 
                       'P02_seperator', 'P02_encoding', 'P02_headfile_info', 
                       'P02_headfile_name', 'P02_firstline', 'P02_sql']
    
    # 03_SQL_Analysis_multi
    sslist = sslist + ['P03_datafile_info', 'P03_datafile_name', 'P03_preview_rows', 
                       'P03_seperator', 'P03_encoding', 'P03_headfile_info', 
                       'P03_headfile_name', 'P03_firstline', 'P03_sql']
    
    # 04_SQL_Analysis_multi
    sslist = sslist + ['P04_datafile_info', 'P04_datafile_name', 'P04_preview_rows', 
                       'P04_seperator', 'P04_encoding', 'P04_headfile_info', 
                       'P04_headfile_name', 'P04_firstline', 'P04_sql', 'P04_chunksize']
    
    # 05_SQL_Analysis_multi
    sslist = sslist + ['P05_datafile_info', 'P05_datafile_name', 'P05_preview_rows', 
                       'P05_seperator', 'P05_encoding', 'P05_headfile_info', 
                       'P05_headfile_name', 'P05_firstline', 'P05_sql']
    
    # 06_SQL_Analysis_multi
    sslist = sslist + ['P06_datafile_info', 'P06_datafile_name', 'P06_preview_rows', 
                       'P06_seperator', 'P06_encoding', 'P06_headfile_info', 
                       'P06_headfile_name', 'P06_firstline', 'P06_sql']
    
    # 99_transaction
    sslist = sslist + ['P99_filefolder', 'P99_filename', 'P99_preview_rows', 
                       'P99_seperator', 'P99_encoding', 'P99_head_folder', 
                       'P99_head_file']
    

    # 全部初始化为None
    for ss in sslist:
        if ss not in st.session_state:
            st.session_state[ss] = None


    # 个别参数重新赋值
    if st.session_state.P01_datafile_info==None:
        st.session_state.P01_datafile_info = r'./'
    if st.session_state.P01_preview_rows==None:
        st.session_state.P01_preview_rows = 30
    if st.session_state.P01_seperator==None:
        st.session_state.P01_seperator = ','
    if st.session_state.P01_encoding==None:
        st.session_state.P01_encoding = 'utf-8'

    if st.session_state.P02_datafile_info==None:
        st.session_state.P02_datafile_info = r'./'
    if st.session_state.P02_preview_rows==None:
        st.session_state.P02_preview_rows = 30
    if st.session_state.P02_seperator==None:
        st.session_state.P02_seperator = ','
    if st.session_state.P02_encoding==None:
        st.session_state.P02_encoding = 'utf-8'

    if st.session_state.P03_datafile_info==None:
        st.session_state.P03_datafile_info = r'./'
    if st.session_state.P03_preview_rows==None:
        st.session_state.P03_preview_rows = 30
    if st.session_state.P03_seperator==None:
        st.session_state.P03_seperator = ','
    if st.session_state.P03_encoding==None:
        st.session_state.P03_encoding = 'utf-8'

    if st.session_state.P04_datafile_info==None:
        st.session_state.P04_datafile_info = r'./'
    if st.session_state.P04_preview_rows==None:
        st.session_state.P04_preview_rows = 30
    if st.session_state.P04_seperator==None:
        st.session_state.P04_seperator = ','
    if st.session_state.P04_encoding==None:
        st.session_state.P04_encoding = 'utf-8'
    if st.session_state.P04_chunksize==None:
        st.session_state.P04_chunksize = 1000

    if st.session_state.P05_datafile_info==None:
        st.session_state.P05_datafile_info = r'./'
    if st.session_state.P05_preview_rows==None:
        st.session_state.P05_preview_rows = 30
    if st.session_state.P05_seperator==None:
        st.session_state.P05_seperator = ','
    if st.session_state.P05_encoding==None:
        st.session_state.P05_encoding = 'utf-8'

    if st.session_state.P06_datafile_info==None:
        st.session_state.P06_datafile_info = r'./'
    if st.session_state.P06_preview_rows==None:
        st.session_state.P06_preview_rows = 30
    if st.session_state.P06_seperator==None:
        st.session_state.P06_seperator = ','
    if st.session_state.P06_encoding==None:
        st.session_state.P06_encoding = 'utf-8'

    if st.session_state.P99_filefolder==None:
        st.session_state.P99_filefolder = r'./'
    if st.session_state.P99_preview_rows==None:
        st.session_state.P99_preview_rows = 30
    if st.session_state.P99_seperator==None:
        st.session_state.P99_seperator = ','
    if st.session_state.P99_encoding==None:
        st.session_state.P99_encoding = 'utf-8'

    print(f'\n\n\n\n  session_state:\n  {st.session_state}\n')



import configparser
class MyParseIni(configparser.ConfigParser):
    def as_dict(self):
        d = dict(self._sections)
        for k in d:
            d[k] = dict(d[k])
        return d
    
if __name__ == "__main__":
    conf = MyParseIni()
    conf.read('file_path', encoding="utf-8")
    config = conf.as_dict()
    print(config)